package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.domain.Producte;
import com.eupmt.lab3.utils.Log;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.IOException;
import java.io.Reader;

public class ProducteDaoManager {

    protected static ProducteDaoManager instance = null;
    private ProducteDAO producteDAO = null;
    private Log log = null;

    public ProducteDaoManager() throws IOException {
        super();
        log = new Log();
        Reader readerDao = Resources.getResourceAsReader("com/eupmt/lab3/persistence/ibatis/daoConfig.xml");
        DaoManager daoManager = DaoManagerBuilder.buildDaoManager(readerDao);
        this.producteDAO = (ProducteDAO) daoManager.getDao(ProducteDAO.class);
//        producteDAO = new ProducteDAOImpl();
    }

    //Metodes de Producte
    public Producte getProducteByNom(String numSerie) throws Exception {
//        log.infoLog(numSerie);
        return producteDAO.getProducteByNumSerie(numSerie);
    }

     public Producte getProducteById(int idProducte) throws Exception {
//        log.infoLog(numSerie);
        return producteDAO.getProducteByNumId(idProducte);
    }

    public void insertProducte(Producte prod) throws Exception {
        producteDAO.insertProducte(prod);
    }

    public void updateProducte(Producte prod) throws Exception {
        producteDAO.updateProducte(prod);
    }

    public void deleteProducte(Producte prod) throws Exception {
        producteDAO.deleteProducte(prod);
    }

    public void saveProducte(Producte prod) throws Exception {
        producteDAO.saveProducte(prod);
    }

    // Instancia singleton
    public static ProducteDaoManager getInstance() throws IOException {
        if (instance == null) {
            instance = new ProducteDaoManager();
        }
        return instance;
    }
}
